package com.mdd.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.pojo.OrderDetail;
import com.mdd.service.OrderDetailService;
import javafx.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 销量排行榜缓存预热任务
 */
@Component
public class TopCacheTask {

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 菜品销量排行榜缓存预热
     */
    private void dishTop() {
        //查询出所有关于菜品订单细节信息
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(OrderDetail::getDishId);
        List<OrderDetail> details = orderDetailService.list(queryWrapper);
        //按菜品id进行分组
        Map<Long, List<OrderDetail>> map = details.stream().collect(Collectors.groupingBy(OrderDetail::getDishId));
        //根据销量进行排序(使用stream流)
        ArrayList<Pair<Long, Integer>> arrayList = new ArrayList<>();
        map.keySet().forEach(id -> {
            arrayList.add(new Pair<>(id, map.get(id).size()));
        });
        List<Pair<Long, Integer>> sortDishs = arrayList.stream().sorted((o1, o2) -> {
            return o2.getValue() - o1.getValue();
        }).limit(20).collect(Collectors.toList());
        //将销量数据缓存到redis中
        ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
        String key = "top:dish:total";
        sortDishs.forEach(pair -> {
            zSetOperations.add(key, pair.getKey().toString(), pair.getValue());
        });
        //设置过期时间
        redisTemplate.expire(key,1,TimeUnit.DAYS);
    }

    /**
     * 套餐销量排行榜缓存预热
     */
    private void setmealTop() {
        //查询出所有关于菜品订单细节信息
        LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(OrderDetail::getSetmealId);
        List<OrderDetail> details = orderDetailService.list(queryWrapper);
        //按菜品id进行分组
        Map<Long, List<OrderDetail>> map = details.stream().collect(Collectors.groupingBy(OrderDetail::getSetmealId));
        //根据销量进行排序(使用stream流)
        ArrayList<Pair<Long, Integer>> arrayList = new ArrayList<>();
        map.keySet().forEach(id -> {
            arrayList.add(new Pair<>(id, map.get(id).size()));
        });
        List<Pair<Long, Integer>> sortDishs = arrayList.stream().sorted((o1, o2) -> {
            return o2.getValue() - o1.getValue();
        }).limit(20).collect(Collectors.toList());
        //将销量数据缓存到redis中
        ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
        String key = "top:setmeal:total";
        sortDishs.forEach(pair -> {
            zSetOperations.add(key, pair.getKey().toString(), pair.getValue());
        });
        redisTemplate.expire(key,1,TimeUnit.DAYS);
    }
}
